package xutils

import (
	"bytes"
	"encoding/hex"
	"gitee.com/igolang/pkg/xjson"
	"github.com/mozillazg/go-pinyin"
	"math/rand"
	"strings"
	"time"
	"unicode"
	"unicode/utf8"
)

// GetFirstLetter 获取字符串首字母
func GetFirstLetter(s string) string {
	runes := []rune(s)
	if len(runes) == 0 {
		return ""
	}
	firstRune := runes[0]

	if unicode.Is(unicode.Han, firstRune) {
		args := pinyin.NewArgs()
		pys := pinyin.Pinyin(string(firstRune), args)
		if len(pys) > 0 && len(pys[0]) > 0 {
			firstPy := pys[0][0]
			if len(firstPy) > 0 {
				return strings.ToUpper(string(firstPy[0]))
			}
		}
		return ""
	} else if unicode.IsLetter(firstRune) {
		return strings.ToUpper(string(firstRune))
	}

	return ""
}

// GetFirstChar 获取字符串的第一个字符
func GetFirstChar(s string) string {
	if len(s) == 0 {
		return "" // 空字符串返回空
	}

	// 获取第一个字符的 rune 和大小
	firstRune, _ := utf8.DecodeRuneInString(s)
	if firstRune == utf8.RuneError {
		return "" // 如果解码失败，返回空
	}

	// 将 rune 转换为字符串
	return string(firstRune)
}

// Len 自然语言字符串个数，包括中英文等
func Len(s string) int {
	return utf8.RuneCountInString(s)
}

func StrArrayEmpty(arr []string) bool {
	for _, v := range arr {
		if v != "" {
			return false
		}
	}
	return true
}

func ParseMac(mac string) []byte {
	if mac == "" {
		return nil
	}
	macArr := make([]string, 0)
	if strings.Contains(mac, ":") {
		macArr = strings.Split(mac, ":")
	} else if strings.Contains(mac, "-") {
		macArr = strings.Split(mac, "-")
	} else {
		if len(mac)%2 != 0 {
			return bytes.NewBufferString(mac).Bytes()
		}
		for i := 0; i < len(mac); {
			s := mac[i : i+2]
			macArr = append(macArr, s)
			i += 2
		}
	}
	if len(macArr) == 0 {
		return nil
	}
	result := make([]byte, 0)
	for _, v := range macArr {
		b, err := hex.DecodeString(v)
		if err != nil {
			return nil
		}
		result = append(result, b[0])
	}

	return result
}

func Bytes2Mac(b []byte) string {
	s := ""
	for _, v := range b {
		s = s + hex.EncodeToString([]byte{v}) + ":"
	}
	s = strings.TrimRight(s, ":")
	return s
}

func FormatStrJson(b []byte) []byte {
	if len(b) == 0 {
		return b
	}
	b = bytes.Trim(b, `"`)
	if IsJson(b) {
		return b
	}

	b = bytes.ReplaceAll(b, []byte(`\`), []byte{})
	return b
}

func IsJson(b []byte) bool {
	return xjson.Valid(b)
}

func RandStr(length int) string {
	str := "yzABCDEFGHIJKLlmnopq78STUrstuvwxPQR3456VWXYZ0129MNOabcdefghijk"
	b := []byte(str)
	var result []byte
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < length; i++ {
		result = append(result, b[r.Intn(len(b))])
	}
	return string(result)
}

func ParseTag(tag string) map[string]any {
	result := make(map[string]any)
	tagArr := strings.Split(tag, ";")
	for _, v := range tagArr {
		vv := strings.Split(v, ":")
		if len(vv) < 2 {
			result[vv[0]] = true
			continue
		}
		result[vv[0]] = vv[1]
	}

	return result
}

// FirstUpper 字符串首字母大写
func FirstUpper(s string) string {
	if s == "" {
		return ""
	}
	return strings.ToUpper(s[:1]) + s[1:]
}

// FirstLower 字符串首字母小写
func FirstLower(s string) string {
	if s == "" {
		return ""
	}
	return strings.ToLower(s[:1]) + s[1:]
}
